<?php
declare (strict_types = 1);

namespace app\user\service\teacher\admin;

use app\admin\logic\system\Config as SystemConfig;
use app\common\service\BaseService;
use app\user\logic\teacher\admin\TeacherLevel as TeacherLevelLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\facade\Request;

/**
 * 用户等级管理-后台
 */
class TeacherLevel extends BaseService
{
    /**
     * 机构等级列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     * @return   [type]                         [description]
     */
    public function index()
    {
        // 搜索功能
        $map  = [];
        $data = app(TeacherLevelLogic::class)->getList($map);
        // 定义字段
        $keyList = [
            'title'           => ListItem::make()->title('等级名称'),
            'icon'            => ListItem::make('custom')->title('等级图标'),
            'update_time'     => ListItem::make()->title('最后操作时间'),
        ];

        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;

        $list->datas = $data;

        return $list->send();
    }

    /**
     * 新建
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     * @param    Request                        $request [description]
     * @return   [type]                                  [description]
     */
    public function create()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $re   = app(TeacherLevelLogic::class)->createLevel($data);
            return $this->parseToData($re->toArray(), 1, '添加成功');
        }

        /* 创建课程界面配置 */
        $keyList = $this->keyListInit();
        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey = $keyList;

        $form->saveMethod  = 'POST';
        $url               = '/user/admin/teacher/level/create';
        $form->savePostUrl = $url;

        return $form->send();

    }

    /**
     * 编辑
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     * @param    [type]                         $id [description]
     * @return   [type]                             [description]
     */
    public function edit($id)
    {
        if (Request::isPut()) {
            $data  = Request::put();
            $video = app(TeacherLevelLogic::class)->editLevel($id, $data);

            return $this->parseToData($video->toArray(), 1, '编辑成功');
        }

        $data = app(TeacherLevelLogic::class)->with(['icon_url'])->findOrFail($id);
        // 创建课程界面配置
        $keyList = $this->keyListInit();
        // 设置表单
        $form = app(PageForm::class);
        
        // 表单字段
        $form->datas       = $data;
        $form->pageKey     = $keyList;
        $form->saveMethod  = 'PUT';
        $form->savePostUrl = '/user/admin/teacher/level/' . $id . '/edit';

        return $form->send();
    }

    /**
     * 表单字段初始化
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     */
    protected function keyListInit()
    {
        // 分成比例
        // $key    = 'split.config';
        // $config = SystemConfig::get($key);
        // $max    = $config['platform'] + $config['school'];
        // 定义字段
        return [
            'title'           => FormItem::make()->title('等级名称')->maxlength(20)->required(),
            'icon'            => FormItem::make('custom')->title('等级图标')->limit(1)->help('建议宽高尺寸为16px*16px'),
        ];
    }

    /**
     * 删除等级
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     * @param    int                            $id [description]
     * @return   [type]                             [description]
     */
    public function delete(int $id)
    {
        $res = app(TeacherLevelLogic::class)->deleteLevel($id);

        return $this->parseToData($res, 1, '删除成功');
    }
}
